/**
 * created by 小卷毛, 2021-05-08
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
@file:Suppress("unused")

package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.model.CommonData
import com.rzico.sbl.model.CommonModel
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import com.xinnuo.common_ui.viewmodel.BaseViewModel
import io.reactivex.Flowable
import java.util.concurrent.TimeUnit

/**
 * 描 述：公共请求方法
 * 作 者：小卷毛  2021-05-08 08:41
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
open class CommonViewModel : BaseViewModel() {

    /**
     * 公用的请求并解析CommonData的方法
     * 使用方式：
     *    requestCommon(
     *        requestUrl = 请求url,
     *        params = arrayOf(..),
     *        onSuccess = {},
     *        onFinally = {}
     *    )
     */
    fun requestCommon(
        isLoading: Boolean = false,
        isMultipart: Boolean = false,
        requestUrl: String,
        requestMethod: HttpMethod = HttpMethod.POST,
        params: Array<Pair<String, Any>> = arrayOf(),
        onSuccess: (ArrayList<CommonData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) {
        if (requestMethod == HttpMethod.POST) {
            mDisposables.add(
                OkGo.post<BaseResponse<ArrayList<CommonData>>>(requestUrl)
                    .isMultipart(isMultipart)
                    .params(getParam(params))
                    .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
                    .adapt(FlowableBody())
                    .compose(applyFlowableSchedulers())
                    .doOnSubscribe { if (isLoading) showLoading() }
                    .doFinally { if (isLoading) dismissLoading() }
                    .map { it.data }
                    .subscribeByFinally(
                        event = this,
                        onSuccess = onSuccess,
                        onFinally = onFinally
                    )
            )
        } else {
            mDisposables.add(
                OkGo.get<BaseResponse<ArrayList<CommonData>>>(requestUrl)
                    .params(getParam(params))
                    .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
                    .adapt(FlowableBody())
                    .compose(applyFlowableSchedulers())
                    .doOnSubscribe { if (isLoading) showLoading() }
                    .doFinally { if (isLoading) dismissLoading() }
                    .map { it.data }
                    .subscribeByFinally(
                        event = this,
                        onSuccess = onSuccess,
                        onFinally = onFinally
                    )
            )
        }
    }

    /**
     * 公用的请求并解析CommonModel的方法
     * 使用方式：
     *    requestModel(
     *        requestUrl = 请求url,
     *        params = arrayOf(..),
     *        onSuccess = {},
     *        onFinally = {}
     *    )
     */
    fun requestModel(
        isLoading: Boolean = false,
        isMultipart: Boolean = false,
        requestUrl: String,
        requestMethod: HttpMethod = HttpMethod.POST,
        params: Array<Pair<String, Any>> = arrayOf(),
        onSuccess: (ArrayList<CommonData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) {
        if (requestMethod == HttpMethod.POST) {
            mDisposables.add(
                OkGo.post<BaseResponse<CommonModel>>(requestUrl)
                    .isMultipart(isMultipart)
                    .params(getParam(params))
                    .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
                    .adapt(FlowableBody())
                    .compose(applyFlowableSchedulers())
                    .doOnSubscribe { if (isLoading) showLoading() }
                    .doFinally { if (isLoading) dismissLoading() }
                    .map { it.data.data }
                    .subscribeByFinally(
                        event = this,
                        onSuccess = onSuccess,
                        onFinally = onFinally
                    )
            )
        } else {
            mDisposables.add(
                OkGo.get<BaseResponse<CommonModel>>(requestUrl)
                    .params(getParam(params))
                    .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
                    .adapt(FlowableBody())
                    .compose(applyFlowableSchedulers())
                    .doOnSubscribe { if (isLoading) showLoading() }
                    .doFinally { if (isLoading) dismissLoading() }
                    .map { it.data.data }
                    .subscribeByFinally(
                        event = this,
                        onSuccess = onSuccess,
                        onFinally = onFinally
                    )
            )
        }
    }

    /**
     * 定时倒计时
     */
    fun countDown(count: Long = 180): Flowable<Long> =
        Flowable
            .intervalRange(
                0,
                count + 1,
                0,
                1,
                TimeUnit.SECONDS
            )
            .compose(applyFlowableSchedulers())

    /**
     * 取消请求
     */
    fun cancelRequest() = mDisposables.clear()

}